package com.zuipin.controller;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zuipin.entity.RedPacket;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IRedPacketService;
import com.zuipin.util.Pagination;
import com.zuipin.vo.RedPacketDetailVo;
import com.zuipin.vo.RedPacketShowVo;
import com.zuipin.vo.RedPacketVo;
import com.zuipin.vo.SearchCanUseVo;
import com.zuipin.vo.SendRedPacketResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/** 
 * @ClassName: RedPackServiceImpl 
 * @author wuyicheng
 * @time 2017年3月27日下午4:04:09 
 */
@Api(value = "红包", description = "红包-wuyicheng")
@RestController
@RequestMapping(value = "/member/redPacket")
public class RedPackController {
	@Resource
	private IRedPacketService redPacketService;
	
	@RequestMapping(value = "/countRedPack", method = RequestMethod.GET)
	@ApiOperation(value = "统计当前可用红包个数-wuyicheng", notes = "统计当前可用红包个数-wuyicheng")
	public Result<Map<String, Object>> countRedPack(@ApiParam(required = true, name = "memberId", value = "会员id") @RequestParam Long memberId,
			@ApiParam(required = true, name = "sysId", value = "系统id") @RequestParam Long sysId) {
		Result<Map<String, Object>> result = new Result<>();
		long nums = redPacketService.countRedPacket(memberId, sysId);
		Map<String, Object> map = new HashMap<>();
		map.put("total", nums);
		result.setData(map);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/searchRedPacket", method = RequestMethod.GET)
	@ApiOperation(value = "查询所有红包-wuyicheng", notes = "查询所有红包-wuyicheng")
	@Valids({ @Valid(name = "memberId", required = true), @Valid(name = "sysId", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "用户名", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"), })
	public PageResult<List<RedPacket>> searchRedPacket(@RequestParam Long memberId, @RequestParam Long sysId) {
		PageResult<List<RedPacket>> result = new PageResult<>();
		RedPacket redPacket = new RedPacket();
		redPacket.setMemberId(memberId);
		redPacket.setSysId(sysId);
		List<RedPacket> list = redPacketService.findList(redPacket);
		if (list != null && list.size() > 0) {
			result.setTotal_record(list.size());
		}
		result.setData(list);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/searchRedPacketByPage", method = RequestMethod.GET)
	@ApiOperation(value = "分页查询红包-wuyicheng", notes = "分页查询红包-wuyicheng")
	@Valids({ @Valid(name = "memberId", required = true), @Valid(name = "sysId", required = true), @Valid(name = "pageNumber", required = true),
			@Valid(name = "pageSize", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "用户ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public PageResult<List<RedPacket>> searchRedPacketByPage(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam Integer pageNumber,
			@RequestParam Integer pageSize) {
		PageResult<List<RedPacket>> result = new PageResult<>();
		RedPacket redPacket = new RedPacket();
		redPacket.setMemberId(memberId);
		redPacket.setSysId(sysId);
		Pagination page = new Pagination();
		page.setPageNumber(pageNumber);
		page.setPageSize(pageSize);
		List<RedPacket> list = redPacketService.findListByPage(redPacket, page);
		if (list != null && list.size() > 0) {
			result.setTotal_record(list.size());
		}
		result.setData(list);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/doActivateByCodeAndPwd", method = RequestMethod.POST)
	@ApiOperation(value = "优惠劵代码和优惠劵密码激活优惠券-wuyicheng", notes = "优惠劵代码和优惠劵密码激活优惠券-wuyicheng")
	@Valids({ @Valid(name = "memberId", required = true), @Valid(name = "sysId", required = true), @Valid(name = "activateCode", required = true),
			@Valid(name = "activatePassword", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "activateCode", value = "优惠劵代码", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "activatePassword", value = "密码", required = true, dataType = "string", paramType = "query"), })
	public Result<Map<String, Object>> doActivateByCodeAndPwd(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam String activateCode,
			@RequestParam String activatePassword) throws ParseException, CustomException {
		Result<Map<String, Object>> result = new Result<>();
		result = redPacketService.doActivateByCodeAndPwd(memberId, sysId, activateCode, activatePassword);
		return result;
	}
	
	@RequestMapping(value = "/doActivateBySerialNumber", method = RequestMethod.POST)
	@ApiOperation(value = "激活码激活红包-wuyicheng", notes = "激活码激活红包-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1000-红包已经激活" + "</br>1001-参数异常" + "</br>1004-系统异常"
			+ "</br>3013-红包已经派完" + "</br>4018-红包不存在" + "</br>4020-规则不存在或者禁用" + "</br>4041-红包过期" + "</br>4042-当前等级无法领取" + "</br>4043-领取数量超过上限" + "</br>4060-规则被禁用"
			+ "</br>4061-红包领取时间过期")
	@Valids({ @Valid(name = "memberId", required = true), @Valid(name = "sysId", required = true), @Valid(name = "serialNumber", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "serialNumber", value = "优惠劵序列号", required = true, dataType = "string", paramType = "query"), })
	public Result<Map<String, Object>> doActivateBySerialNumber(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam String serialNumber)
			throws ParseException, CustomException {
		Result<Map<String, Object>> result = redPacketService.doActivateBySerialNumber(memberId, sysId, serialNumber);
		return result;
	}
	
	@RequestMapping(value = "/doActivateByRuleId", method = RequestMethod.POST)
	@ApiOperation(value = "根据规则ID激活红包-wuyicheng", notes = "根据规则ID激活红包-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1000-红包已经激活" + "</br>1001-参数异常" + "</br>1004-系统异常"
			+ "</br>3013-红包已经派完" + "</br>4020-规则不存在或者禁用" + "</br>4041-红包过期" + "</br>4042-当前等级无法领取" + "</br>4043-领取数量超过上限" + "</br>4060-规则被禁用" + "</br>4061-红包领取时间过期")
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "ruleId", value = "红包规则ID", required = true, dataType = "string", paramType = "query"), })
	public Result<Map<String, Object>> doActivateByRuleId(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam Long ruleId) throws ParseException, CustomException {
		Result<Map<String, Object>> result = redPacketService.doActivateByRuleId(memberId, sysId, ruleId);
		return result;
	}
	
	@RequestMapping(value = "/selectCanUseRedPacket", method = RequestMethod.POST)
	@ApiOperation(value = "查询使用红包（后台接口，已废弃）-wuyicheng", notes = "查询使用红包-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>4020-红包规则不存在")
	public Result<List<RedPacketVo>> selectCanUseRedPacket(@RequestBody @ApiParam(value = "查询可用红包") SearchCanUseVo searchCanUseVo) throws ParseException, CustomException {
		Result<List<RedPacketVo>> result = new Result<>(ErrorCode.SUCCESS);
		// List<Map<String, Object>> mapList = redPacketService.getCanUseRedpacket(searchCanUseVo);
		// String josnStr = JSONObject.toJSONString(mapList);
		// List<RedPacketVo> list = JSONObject.parseArray(josnStr, RedPacketVo.class);
		List<RedPacketVo> list = redPacketService.getCanUseRedpacket(searchCanUseVo);
		result.setData(list);
		return result;
	}
	
	@RequestMapping(value = "/toSendRedPacket", method = RequestMethod.POST)
	@ApiOperation(value = "派发红包接口-wuyicheng", notes = "派发红包接口-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>4020-红包规则不存在" + "</br>4060-规则被禁用")
	@Valids({ @Valid(name = "isMemberLevel", required = true, minValue = 0, maxValue = 1), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysUserId", value = "操作者ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "ruleSetId", value = "红包规则ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "isMemberLevel", value = "是否根据会员等级派发1-根据等级派发，0根据会员手机号派发", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "acceptor", value = "会员账号或者会员等级，用逗号隔开", required = true, dataType = "string", paramType = "query"), })
	public Result<SendRedPacketResult> toSendRedPacket(Long sysUserId, Long sysId, Long ruleSetId, Short isMemberLevel, String acceptor) throws ParseException, CustomException {
		Result<SendRedPacketResult> result = new Result<>();
		result = redPacketService.toSendRedPacket(sysUserId, sysId, ruleSetId, isMemberLevel, acceptor);
		return result;
	}
	
	@RequestMapping(value = "/toExportSerialNumber", method = RequestMethod.POST)
	@ApiOperation(value = "导出红包序列号接口-wuyicheng", notes = "导出红包序列号接口-wuyicheng" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>4020-红包规则不存在")
	@ApiImplicitParams({ @ApiImplicitParam(name = "ruleSetIds", value = "规则ID集合,用逗号隔开", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"), })
	public Result<List<Map<String, Object>>> toExportSerialNumber(String ruleSetIds, Long sysId) throws ParseException, CustomException {
		Result<List<Map<String, Object>>> result = new Result<>(ErrorCode.SUCCESS);
		result = redPacketService.toExportSerialNumber(ruleSetIds, sysId, result);
		return result;
	}
	
	@RequestMapping(value = "/findRedPacketListPage", method = RequestMethod.POST)
	@ApiOperation(notes = "后台查询红包明细列表-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "后台查询红包明细列表-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "pageNumber", required = true, minValue = 1), @Valid(name = "status", required = false, regex = "INACTIVE|UNUSED|FREEZE|USED|EXPIRED"),
			@Valid(name = "pageSize", required = true, minValue = 1), })
	public PageResult<List<RedPacketDetailVo>> findRedPacketListPage(@ApiParam(name = "sysId", value = "系统Id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "pageSize", value = "页长", required = true) @RequestParam(required = true) Integer pageSize,
			@ApiParam(name = "pageNumber", value = "页码", required = true) @RequestParam(required = true) Integer pageNumber,
			@ApiParam(name = "status", value = "状态   INACTIVE:未派发, UNUSED:已派发，未使用, FREEZE:已禁用 ,USED：已使用,EXPIRED:已过期", required = false) @RequestParam(required = false) String status,
			@ApiParam(name = "phoneOrEmail", value = "会员帐号", required = false) @RequestParam(required = false) String phoneOrEmail,
			@ApiParam(name = "id", value = "红包编码", required = false) @RequestParam(required = false) Long id,
			@ApiParam(name = "orderNo", value = "订单编码", required = false) @RequestParam(required = false) String orderNo,
			@ApiParam(name = "ruleId", value = "规则编码", required = false) @RequestParam(required = false) Long ruleId) {
		PageResult<List<RedPacketDetailVo>> list = redPacketService.findRedPacketListPage(sysId, pageSize, pageNumber, status, phoneOrEmail, id, orderNo, ruleId);
		return list;
		
	}
	
	@RequestMapping(value = "/findRedPacketDetailById", method = RequestMethod.POST)
	@ApiOperation(notes = "查询红包详情-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询红包详情-xujinding")
	@ResponseBody
	public Result<RedPacketDetailVo> findRedPacketDetailById(@ApiParam(name = "sysId", value = "系统id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "id", value = "Id", required = true) @RequestParam(required = true) Long id)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		RedPacketDetailVo redPacketDetailVo = redPacketService.findRedPacketDetailById(id, sysId);
		Result<RedPacketDetailVo> result = new Result<>(ErrorCode.SUCCESS);
		result.setData(redPacketDetailVo);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/searchRedPacketByStatus", method = RequestMethod.GET)
	@ApiOperation(value = "查询会员的红包-(状态查询)", notes = "查询的会员红包-(状态查询)" + "</br>返回码" + "</br>0-成功" + "</br>2000-没查询到红包")
	@Valids({ @Valid(name = "memberId", required = true), @Valid(name = "sysId", required = true), @Valid(name = "status", regex = "UNUSED|USED|EXPIRED", required = true) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "memberId", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "status", value = "'UNUSED','USED','EXPIRED'状态:未使用，已使用，已过期", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public PageResult<List<RedPacketShowVo>> searchRedPacketByStatus(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam String status,
			@Inject @ApiIgnore Pagination page) {
		PageResult<List<RedPacketShowVo>> result = redPacketService.searchRedPacketByStatus(memberId, sysId, status, page);
		return result;
	}
	
	/*@RequestMapping(value = "/searchUnusedRedPacket", method = RequestMethod.GET)
	@ApiOperation(value = "查询会员未使用红包", notes = "查询会员未使用红包" + "</br>返回码" + "</br>0-成功" + "</br>2000-没查询到红包")
	@Valids({ @Valid(name = "memberId", required = true), @Valid(name = "sysId", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "用户名", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = false, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = false, dataType = "integer", paramType = "query"), })
	public PageResult<List<RedPacketShowVo>> searchUnusedRedPacket(@RequestParam Long memberId, @RequestParam Long sysId, @Inject @ApiIgnore Pagination page) {
		// 会员未使用红包中 存在过期红包 要用[当前时间]去和[红包最后使用时间]判断 这个红包是否已经过期
		PageResult<List<RedPacketShowVo>> result = redPacketService.searchUnusedRedPacket(memberId, sysId, page);
		return null;
	}
	
	@RequestMapping(value = "/searchExpiredRedPacket", method = RequestMethod.GET)
	@ApiOperation(value = "查询会员过期红包", notes = "查询会员过期红包" + "</br>返回码" + "</br>0-成功" + "</br>2000-没查询到红包")
	@Valids({ @Valid(name = "memberId", required = true), @Valid(name = "sysId", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "memberId", value = "用户名", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = false, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = false, dataType = "integer", paramType = "query"), })
	public PageResult<List<RedPacketShowVo>> searchExpiredRedPacket(@RequestParam Long memberId, @RequestParam Long sysId, @Inject @ApiIgnore Pagination page) {
		// 会员未使用红包中 存在过期红包 要用[当前时间]去和[红包最后使用时间]判断 这个红包是否已经过期
		PageResult<List<RedPacketShowVo>> result = redPacketService.searchExpiredRedPacket(memberId, sysId, page);
		return null;
	}
	*/
}
